package colin.spring.boot.sample.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import colin.spring.boot.sample.web.bean.UserInfoBean;
import colin.spring.boot.sample.web.pojo.UserEntity;
import colin.spring.boot.sample.web.service.UserService;

@Controller
public class UserController extends WebMvcConfigurerAdapter {
	@Autowired
	private UserService userService;
	/**
	 * 
	 * 方法描述：显示用户的列表页面 注意事项：
	 * 
	 * @return
	 * @Exception 异常对象
	 */
	@RequestMapping(value = "showUserInfo.html", method = RequestMethod.GET)
	public ModelAndView listUserInfo() {
		ModelAndView mvAndView = new ModelAndView("userInfoList");
		// 加载全部用户信息
		List<UserEntity> userList = userService.getAllUsersInfo();
		if (userList != null) {
			mvAndView.addObject("userList", userList);
		}
		return mvAndView;
	}

	/**
	 * 
	 * 方法描述：保存用户的信息 注意事项：
	 * 
	 * @param username
	 * @param password
	 * @return
	 * @Exception 异常对象
	 */
	@ResponseBody
	@RequestMapping(value = "/createUserInfo.action", method = RequestMethod.POST)
	public Object createUserInfo(
			@ModelAttribute("user") @Valid UserInfoBean userBean, Errors errors) {
		Map<String, Object> result = new HashMap<String, Object>();
		if (errors.hasErrors()) {
			Map<String, Object> errorMap = new HashMap<>();
			for (FieldError error : errors.getFieldErrors()) {
				errorMap.put(error.getField(), error.getDefaultMessage());
			}
			result.put("errors", errorMap);
			result.put("isSuccess", false);
		} else {
			userService.createUserInfo(userBean.getUsername(),
					userBean.getPassword());
			result.put("isSuccess", true);
		}
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "findUserInfo.action", method = RequestMethod.POST)
	public Object findUserInfo(@RequestParam String username,
			@RequestParam String email, @RequestParam String sex) {
		if (sex.equals("0")) {
			sex = "";
		}
		Map<String, Object> params = new HashMap<String, Object>();
		if (username != null && !username.equals("")) {
			params.put("username", username);
		}
		if (email != null && !email.equals("")) {
			params.put("email", email);
		}
		if (sex != null && !sex.equals("")) {
			params.put("sex", sex);
		}
		return this.userService.findUserInfo(params);
	}

	/**
	 * 
	 * 方法描述： 注意事项：
	 * 
	 * @param truename
	 * @return
	 * @Exception 异常对象
	 */
	@ResponseBody
	@RequestMapping(value = "findUsername.action", method = RequestMethod.POST)
	public Object findUsername(@RequestParam String truename) {
		if (truename.equals("")) {
			return null;
		} else {
			return userService.findUsername(truename);
		}
	}
}
